package com.jwsoft.manager.core.gateway.controller;

import com.bifang.engine.assistant.core.exception.AppException;
import com.bifang.engine.protect.core.annotation.Crypto;
import com.jwsoft.manager.common.vo.eduSyncJhApply.EduSyncJhApplyVO;
import com.jwsoft.manager.common.vo.eduSyncJhApprove.EduSyncJhApproveVO;
import com.jwsoft.manager.common.vo.eduSyncJhDelete.*;
import com.jwsoft.manager.common.vo.eduSyncJhEnroll.EduSyncJhEnrollVO;
import com.jwsoft.manager.core.integration.EduSyncJhApplyIntegration;
import com.jwsoft.manager.core.integration.EduSyncJhApproveIntegration;
import com.jwsoft.manager.core.integration.EduSyncJhDeleteIntegration;
import com.github.pagehelper.PageInfo;
import com.bifang.engine.assistant.core.domain.AppResponse;
import com.jwsoft.manager.core.integration.EduSyncJhEnrollIntegration;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import com.bifang.engine.protect.core.annotation.Idempotent;
import org.springframework.validation.annotation.Validated;
import com.bifang.assistant.common.constants.Validators;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Date;

/**
 * 金华市报名删除同步情况表网关
 *
 * @author zhiyuan
 * @since 2024-04-08
 */
@RestController
@RequestMapping(value = "/doc/api")
@Tag(name = "金华市报名删除同步情况表", description = "金华市报名删除同步情况表")
public class EduSyncJhDeleteController {

    @Autowired
    public EduSyncJhDeleteIntegration eduSyncJhDeleteIntegration;


    @Autowired
    public EduSyncJhApplyIntegration applyIntegration;


    @Autowired
    public EduSyncJhApproveIntegration approveIntegration;


    @Autowired
    public EduSyncJhEnrollIntegration enrollIntegration;

    @Operation(summary = "金华市报名学生信息删除同步-向外暴露接口",description = "金华市报名学生信息删除同步-向外暴露接口")
    @PostMapping(value = "/syncJhDelete0001", produces = "application/json;charset=UTF-8")
    @Crypto
    public AppResponse syncJhDeleteByType(@RequestBody @Validated EduSyncJhDeleteQueryVO vo,HttpServletRequest request) {
        eduSyncJhDeleteIntegration.syncJhDeleteByType(vo,request);
        return AppResponse.successMsg("数据已删除！");
    }

    @Operation(summary = "金华市报名学生信息删除同步-内部接口",description = "金华市报名学生信息删除同步-内部接口")
    @PostMapping(value = "/syncJhDelete0002", produces = "application/json;charset=UTF-8")
    @Crypto
    public AppResponse syncJhDeleteByType2(@RequestBody @Validated EduSyncJhDeleteQueryVO vo) {
        eduSyncJhDeleteIntegration.syncJhDeleteByType2(vo);
        return AppResponse.successMsg("数据已删除！");
    }

    @Operation(summary = "金华市根据类型查询学生信息是否存在-向外暴露接口",description = "金华市根据类型查询学生信息是否存在-向外暴露接口")
    @PostMapping(value = "/syncJhQuery0001", produces = "application/json;charset=UTF-8")
    @Crypto
    public AppResponse<EduSyncExistInfo> querySyncInfo(@RequestBody @Validated EduSyncJhDeleteQueryVO vo, HttpServletRequest request) {
        String appId = request.getHeader("appId");
        if (StringUtils.isBlank(appId)){
            appId = request.getHeader("appid");
        }
        if (StringUtils.isBlank(appId)) {
            throw new AppException("应用id不能为空！");
        }
        vo.setAppId(Long.parseLong(appId));
        return AppResponse.successData(eduSyncJhDeleteIntegration.querySyncInfo(vo));
    }

    @Operation(summary = "金华市学生报名信息同步-内部接口",description = "金华市学生报名信息同步-内部接口")
    @PostMapping(value = "/syncSaveJhApply0002", produces = "application/json;charset=UTF-8")
    public AppResponse saveSyncApplyInfo2(@RequestBody @Validated EduSyncJhApplyVO vo) {
        if (StringUtils.isBlank(vo.getApplyDate())) {
            throw new AppException("报名时间不能为空！");
        }else{
            //将时间格式转换为时间搓
            if (isValidDate(vo.getApplyDate())) {
                //如果是时间，转换为时间戳
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = null;
                try {
                    date = dateFormat.parse(vo.getApplyDate());
                    long timestamp = date.getTime();
                    vo.setApplyTime(timestamp);
                } catch (ParseException e) {
                    throw new AppException("报名时间格式不正确！ 正确格式如:2024-01-01 12:10:50    ");
                }
            }else{
                throw new AppException("报名时间格式不正确！ 正确格式如:2024-01-01 12:10:50    ");
            }
        }
        applyIntegration.saveSyncApplyInfo2(vo);
        return AppResponse.successMsg("学生报名信息同步成功！");
    }

    @Operation(summary = "金华市学生报名信息同步-向外暴露接口",description = "金华市学生报名信息同步-向外暴露接口")
    @PostMapping(value = "/syncSaveJhApply0001", produces = "application/json;charset=UTF-8")
    @Crypto
    public AppResponse saveSyncApplyInfo(@RequestBody @Validated EduSyncJhApplyVO vo, HttpServletRequest request) {
        if (StringUtils.isBlank(vo.getApplyDate())) {
            throw new AppException("报名时间不能为空！");
        }else{
            //将时间格式转换为时间搓
            if (isValidDate(vo.getApplyDate())) {
                //如果是时间，转换为时间戳
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = null;
                try {
                    date = dateFormat.parse(vo.getApplyDate());
                    long timestamp = date.getTime();
                    vo.setApplyTime(timestamp);
                } catch (ParseException e) {
                    throw new AppException("报名时间格式不正确！ 正确格式如:2024-01-01 12:10:50    ");
                }
            }else{
                throw new AppException("报名时间格式不正确！ 正确格式如:2024-01-01 12:10:50    ");
            }
        }
        applyIntegration.saveSyncApplyInfo(vo,request);
        return AppResponse.successMsg("学生报名信息同步成功！");
    }


    @Operation(summary = "金华市学生审核信息同步-向外暴露接口",description = "金华市学生审核信息同步-向外暴露接口")
    @PostMapping(value = "/syncSaveJhApprove0001", produces = "application/json;charset=UTF-8")
    @Crypto
    public AppResponse saveSyncApproveInfo(@RequestBody @Validated EduSyncJhApproveVO vo, HttpServletRequest request) {
        approveIntegration.saveSyncApproveInfo(vo,request);
        return AppResponse.successMsg("学生审核信息同步成功！");
    }

    @Operation(summary = "金华市学生审核信息同步-内部接口",description = "金华市学生审核信息同步-内部接口")
    @PostMapping(value = "/syncSaveJhApprove0002", produces = "application/json;charset=UTF-8")
    public AppResponse saveSyncApproveInfo2(@RequestBody @Validated EduSyncJhApproveVO vo) {
        approveIntegration.saveSyncApproveInfo2(vo);
        return AppResponse.successMsg("学生审核信息同步成功！");
    }

    @Operation(summary = "金华市学生录取信息同步-向外暴露接口",description = "金华市学生录取信息同步-向外暴露接口")
    @PostMapping(value = "/syncSaveJhEnroll0001", produces = "application/json;charset=UTF-8")
    @Crypto
    public AppResponse saveSyncEnrollInfo(@RequestBody @Validated EduSyncJhEnrollVO vo, HttpServletRequest request) {
        enrollIntegration.saveSyncEnrollInfo(vo,request);
        return AppResponse.successMsg("学生录取信息同步成功！");
    }

    @Operation(summary = "金华市学生录取信息同步-内部接口",description = "金华市学生录取信息同步-内部接口")
    @PostMapping(value = "/syncSaveJhEnroll0002", produces = "application/json;charset=UTF-8")
    public AppResponse saveSyncEnrollInfo2(@RequestBody @Validated EduSyncJhEnrollVO vo) {
        enrollIntegration.saveSyncEnrollInfo2(vo);
        return AppResponse.successMsg("学生录取信息同步成功！");
    }


    private boolean isValidDate(String applyDate) {
        //判断时间是否为yyyy-MM-dd HH:mm:ss 格式
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        try {
            LocalDateTime.parse(applyDate, dtf);
            return true;
        } catch (DateTimeParseException e) {
            return false;
        }
    }

}
